const router = require('koa-router')(),
      {task} = require('../models');

router.prefix('/api/tasks');

router.get('/',async ctx=>{
  ctx.body = await task.getTasks(ctx.user.id);
});

router.post('/',async ctx=>{
  let {content, folderId} = ctx.request.body;
  if(typeof folderId === 'undefined') folderId = null;
  console.log(content,folderId);

  ctx.body = await task.addTask(content,folderId,ctx.user.id);
});

router.put('/:id',async ctx=>{
  let {id} = ctx.params;
  let {content,folderId,recycle} = ctx.request.body;
  console.log(content,folderId,recycle);
  if(typeof content !== 'undefined'){
    ctx.body = await task.updateTaskContentByID(id,content,ctx.user.id);
    return;
  }

  if(typeof folderId !== 'undefined'){
    ctx.body = await task.updateTaskFolderByID(id,folderId,ctx.user.id);
    return;
  }
  
  if(typeof recycle !== 'undefined'){
    ctx.body = await task.recycleTaskByID(id,ctx.user.id);
    return;
  }
});

router.delete('/:id',async ctx=>{
  const {id} = ctx.params;
  const {permanent} = ctx.request.query;

  ctx.body = await task.deleteTaskByID(id,ctx.user.id,permanent);
});


module.exports=router;



